package com.itboye.services.ucenter.facade;

import com.itboye.mybatis.mapper.IAuthGroupMapper;
import com.itboye.mybatis.mapper.IBaseMapper;
import com.itboye.mybatis.mapper.IMemberMapper;
import com.itboye.mybatis.mapper.IUcenterMemberMapper;
import com.itboye.mybatis.model.MemberModel;
import com.itboye.mybatis.model.UcenterMemberModel;
import com.itboye.mybatis.viewmodel.AccountViewModel;
import com.itboye.mybatis.viewmodel.UserAuthGroupAccessViewModel;
import com.itboye.services.ucenter.BaseService;
import com.itboye.services.ucenter.interfaces.IAccountService;
import com.itboye.system.exception.ByServiceException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hebidu on 16/7/21.
 */
@Service("accountService")
public class AccountService extends BaseService<AccountViewModel> implements IAccountService {

    @Resource
    private IMemberMapper memberMapper;

    @Resource
    private IUcenterMemberMapper ucenterMemberMapper;

    @Resource
    private  IAuthGroupMapper authGroupMapper;

    public IBaseMapper<AccountViewModel> getMapper() {
        return null;
    }

    @Transactional(rollbackFor = ByServiceException.class)
    public int insert(AccountViewModel model) throws ByServiceException {

        int effectRows = ucenterMemberMapper.insert(model);
        if(effectRows == 0){
            throw  new ByServiceException("用户信息插入失败");
        }

        int uid = model.getId();
        System.out.println("新增用户 uid = " + uid);

        MemberModel memberModel = model.getMemberModel();

        memberModel.setUid(uid);

        effectRows = memberMapper.insert(memberModel);

//
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        if(effectRows == 0){
            throw  new ByServiceException("基础信息插入失败");
        }

        List<UserAuthGroupAccessViewModel> list = model.getAuthGroupModelList();

        if(list != null) {

            List<UserAuthGroupAccessViewModel> userAuthGroupAccessViewModelList = new ArrayList<UserAuthGroupAccessViewModel>();
            for (int i=0;i<list.size();i++){
                UserAuthGroupAccessViewModel authGroupAccessViewModel = list.get(i);
                authGroupAccessViewModel.setUid(uid);
                userAuthGroupAccessViewModelList.add(authGroupAccessViewModel);
            }
            authGroupMapper.batchInsertUserAccess(userAuthGroupAccessViewModelList);
        }
        return uid;
    }

    public AccountViewModel getInfo(int id)  throws ByServiceException {

        AccountViewModel accountViewModel = new AccountViewModel();


        UcenterMemberModel ucenterMemberModel = ucenterMemberMapper.getInfo(id);

        if(ucenterMemberModel == null){
            return  null;
        }

        accountViewModel.setUcenterMemberModel(ucenterMemberModel);

        MemberModel memberModel = memberMapper.getInfo(accountViewModel.getId());

        //其中一个是null,另一个有的时候抛出异常
        if(memberModel == null){
            throw  new ByServiceException("该数据记录出错!");
        }

        accountViewModel.setMemberModel(memberModel);

        List<UserAuthGroupAccessViewModel> list = authGroupMapper.queryByUid(accountViewModel.getId());

        accountViewModel.setAuthGroupModelList(list);

        return accountViewModel;
    }

    public AccountViewModel getInfoBy(HashMap params) throws ByServiceException {
        AccountViewModel accountViewModel = new AccountViewModel();

        UcenterMemberModel ucenterMemberModel = ucenterMemberMapper.getInfoBy(params);

        if(ucenterMemberModel == null){
            return  null;
        }

        accountViewModel.setUcenterMemberModel(ucenterMemberModel);

        MemberModel memberModel = memberMapper.getInfo(accountViewModel.getId());

        //其中一个是null,另一个有的时候抛出异常
        if(memberModel == null){
            throw  new ByServiceException("该数据记录出错!");
        }

        accountViewModel.setMemberModel(memberModel);

        List<UserAuthGroupAccessViewModel> list = authGroupMapper.queryByUid(accountViewModel.getId());

        accountViewModel.setAuthGroupModelList(list);

        return accountViewModel;
    }



    private static final Logger logger = LogManager.getLogger(AccountService.class.toString());

}
